import 'package:flutter_admin/api/odoo/sale_inquiry_form_odoo_api.dart';
import 'package:flutter_admin/models/odoo/sale_inquiry_form.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';

import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/utils.dart';

class SaleBidProjectFormSelect extends StatefulWidget {
  SaleBidProjectFormSelect();

  @override
  _SaleBidProjectFormSelectState createState() =>
      _SaleBidProjectFormSelectState();
}

class _SaleBidProjectFormSelectState
    extends State<SaleBidProjectFormSelect> {
  SaleInquiryForm saleInquiryForm = SaleInquiryForm();
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  PageModel page = PageModel(orders: [OrderItemModel(column: 'create_date', asc: false)]);
  bool flag = true;
  @override
  void initState() {
    super.initState();
    _query();
  }

  @override
  void dispose() {
    super.dispose();
  }

  _loadData() async {
    saleInquiryForm.state = '1';
    ResponseBodyApi responseBodyApi = await SaleInquiryFormOdooApi.page(
        data: {"page": page, "domainList": saleInquiryForm.toDomainList()});
    if (responseBodyApi.success!) {
      page = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      tableKey.currentState!.loadData(page);
    }
  }

  _query() {
    formKey.currentState?.save();
    tableKey.currentState?.pageTo(0);
    _loadData();
  }

  _reset() {
    saleInquiryForm = SaleInquiryForm();
    formKey.currentState!.reset();
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            width: 160,
            label: '报价单编号',
            value: saleInquiryForm.inquiryNo,
            onSaved: (v) {
              saleInquiryForm.inquiryNo = v;
            },
          ),
          CryInput(
            width: 140,
            label: '业务员',
            value: saleInquiryForm.userName,
            onSaved: (v) {
              saleInquiryForm.userName = v;
            },
          ),
          CryInput(
            width: 160,
            label: '产品名称',
            value: saleInquiryForm.name,
            onSaved: (v) {
              saleInquiryForm.name = v;
            },
          ),
        ],
      ),
    );

    CryDataTable table = CryDataTable(
      key: tableKey,
      title: "数据列表",
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        _loadData();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        _loadData();
      },
      onSelectChanged: (Map selected) {
        if (flag) {
          Navigator.pop(context, selected);
        }
        flag = false;
      },
      columns: [
        DataColumn(label: Text('状态')),
        DataColumn(label: Text('报价单编号')),
        DataColumn(label: Text('报价单位')),
        DataColumn(label: Text('业务员')),
        DataColumn(label: Text('事业部')),
        DataColumn(label: Text('电话')),
        DataColumn(label: Text('邮箱')),
        DataColumn(label: Text('询价单位')),
        DataColumn(label: Text('项目名称')),
        DataColumn(label: Text('客户性质')),
        DataColumn(label: Text('付款方式')),
        DataColumn(label: Text('到货地点')),
        DataColumn(label: Text('是否有目标价')),
        DataColumn(label: Text('合计(元)')),
        DataColumn(label: Text('费用合计（元）')),
        DataColumn(label: Text('毛利润（元）')),
        DataColumn(label: Text('创建人')),
        DataColumn(label: Text('创建时间')),
      ],
      getCells: (m) {
        SaleInquiryForm saleInquiryForm = SaleInquiryForm.fromMap(m);
        return [
          DataCell(Text(saleInquiryForm.state == '1'
              ? '报价'
              : saleInquiryForm.state == '0'
              ? '询价'
              : '--')),
          DataCell(Text(saleInquiryForm.inquiryNo ?? '--')),
          DataCell(Text(saleInquiryForm.companyName ?? '--')),
          DataCell(Text(saleInquiryForm.userName ?? '--')),
          DataCell(Text(saleInquiryForm.deptName ?? '--')),
          DataCell(Text(saleInquiryForm.phone ?? '--')),
          DataCell(Text(saleInquiryForm.email ?? '--')),
          DataCell(Text(saleInquiryForm.partnerName ?? '--')),
          DataCell(Text(saleInquiryForm.projectName ?? '--')),
          DataCell(Text(saleInquiryForm.customerNature ?? '--')),
          DataCell(Text(saleInquiryForm.paymentMethod ?? '--')),
          DataCell(Text(saleInquiryForm.arrivalPlace ?? '--')),
          DataCell(Text(saleInquiryForm.isTargetprice == '1'
              ? '是'
              : saleInquiryForm.isTargetprice == '0'
              ? '否'
              : '--')),
          DataCell(Text(saleInquiryForm.sum ?? '--')),
          DataCell(Text(saleInquiryForm.amountTotal != null
              ? saleInquiryForm.amountTotal.toString()
              : '--')),
          DataCell(Text(saleInquiryForm.grossProfit != null
              ? saleInquiryForm.grossProfit.toString()
              : '--')),
          DataCell(Text(saleInquiryForm.createUid ?? '--')),
          DataCell(Text(Utils.timeUTC(saleInquiryForm.createDate ?? '--'))),
        ];
      },
    );

    CryButtonBar buttonBar = CryButtonBar(
      children: <Widget>[
        CryButtons.query(context, () => _query()),
        CryButtons.reset(context, () => _reset()),
      ],
    );

    var result = Scaffold(
      appBar: AppBar(
        // leading: Builder(
        //   builder: (BuildContext context) {
        //     return SizedBox.shrink();
        //   },
        // ),
        title: Text("选择报价单"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 10),
          form,
          buttonBar,
          Expanded(child: SingleChildScrollView(child: table)),
        ],
      ),
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
